package com.wedo.admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wedo.admin.mapper.AdminMapper;
import com.wedo.admin.pojo.Admin;
import com.wedo.admin.utils.CodecUtils;
import com.wedo.common.pojo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    public Boolean check(String adminName) {
        Admin record = new Admin();
        record.setAdminName(adminName);
        return this.adminMapper.selectCount(record) == 0;
    }


    public Admin queryByAdminName(String adminName, String password) {

        Admin admin = new Admin();
        if (StringUtils.isBlank(adminName) || StringUtils.isBlank(password)) {
            return null;
        }
        admin.setAdminName(adminName);
        Admin result = this.adminMapper.selectOne(admin);

        if (result == null) {
            return null;
        }

        // 获取盐并加密
        password = CodecUtils.md5Hex(password,result.getSalt());

        // 和数据库中的密码比较
        if (StringUtils.equals(password,result.getPassword())) {
            result.setPassword(null);
            return result;
        }
        return null;
    }

    public Boolean addAdmin(Admin admin) {
        if (!check(admin.getAdminName())) {
            return false;
        }

        Admin record = new Admin();
        record.setAdminName(admin.getAdminName());
        //record.setPassword();

        // 1.生成盐
        String salt = CodecUtils.generateSalt();
        record.setSalt(salt);

        // 2.加盐加密
        record.setPassword(CodecUtils.md5Hex(admin.getPassword(), salt));

        this.adminMapper.insertSelective(record);
        return true;
    }

    public PageResult<Admin> queryAdmins(String adminName, Integer page, Integer rows) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        // 只查询id列和adminName列
        example.selectProperties("id","adminName");

        if (!StringUtils.isBlank(adminName)) {
            criteria.andLike("adminName","%" + adminName + "%");
        }

        // 添加分页
        PageHelper.startPage(page, rows);
        List<Admin> admins = this.adminMapper.selectByExample(example);
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(), admins);
    }


    /**
     * 修改密码
     * @param admin
     * @return
     */
    public Boolean updateAdminPassword(Admin admin) {
        admin.setAdminName(null);

        // 1.生成盐
        String salt = CodecUtils.generateSalt();
        admin.setSalt(salt);

        // 2.加盐加密
        admin.setPassword(CodecUtils.md5Hex(admin.getPassword(), salt));

        return this.adminMapper.updateByPrimaryKeySelective(admin) > 0;
    }

    public Admin deleteAdmin(Admin admin) {
        if (this.adminMapper.deleteByPrimaryKey(admin.getId()) > 0) {
            return admin;
        }
        return null;
    }
}
